package org.lanqiao.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.lanqiao.information.Ask;
import org.lanqiao.login.IAdminService;
import org.lanqiao.mapper.AdminMapper;
import org.lanqiao.users.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin queryAdminByNameAndPwd(String loginname, String pass) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("user_name",loginname));
    }

    @Override
    public Admin selectloginname(String loginname) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("user_name",loginname));
    }

    @Override
    public Admin selectid(Integer id) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("user_id",id));

    }

    @Override
    public void upuserdata(Admin admin) {
        adminMapper.updateById(admin);
    }

    @Override
    public void addadmin(Admin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public void deleteuser(String loginname) {
        adminMapper.delete(new QueryWrapper<Admin>().eq("user_name",loginname));
    }

    @Override
    public IPage<Admin> searchPage(Integer pagenum, Integer pagesize) {
        IPage<Admin> page= new Page<>(pagenum,pagesize);
        page = adminMapper.selectPage(page,null);
        return page;
    }


    @Override
    public IPage<Admin> searchByCondition(Integer pagenum, Integer pagesize, Map<String, Object> querMap) {
        IPage<Admin> page= new Page<>(pagenum,pagesize);
        //条件查询
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if (querMap != null){
            if (!StringUtils.isEmpty(querMap.get("loginname"))) {
                queryWrapper.like("user_name", querMap.get("loginname"));
            }
            if (!StringUtils.isEmpty(querMap.get("id"))) {
                queryWrapper.eq("user_id", querMap.get("id"));
            }
        }
        page = adminMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public void changestatus(Admin admin) {
        adminMapper.updateById(admin);
    }


}
